package com.ts.api.exp;

import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.entity.AjaxResult;
import com.ts.api.common.entity.MsgFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 	统一异常处理
 */
@Slf4j
@RestControllerAdvice
public class ExcHandler {

	@ExceptionHandler(Exception.class)
	public AjaxResult exception(HttpServletRequest request, HttpServletResponse response, Throwable e) {
		log.info("出现异常,地址:{},异常:{}", getErrorMsg(request), e.getMessage(), e);
		return AjaxResult.error("未知错误");
	}

	@ExceptionHandler(BizException.class)
	public AjaxResult bizException(HttpServletRequest request, HttpServletResponse response, BizException e) {
		BizEnum bizEnum = e.getBizEnum();
		log.info("业务异常, {}, 业务标识:{}, 业务编号:{}, 业务消息:{}, 异常消息:{}", getErrorMsg(request), bizEnum, bizEnum.getCode(), bizEnum.getMsg(), e.getMessage(), e);
		return AjaxResult.error(e.getBizEnum().getCode(), e.getBizEnum().getMsg());
	}

	@ExceptionHandler(ApiMsgException.class)
	public Object apiMsgException(HttpServletRequest request, HttpServletResponse response, ApiMsgException e) {
		log.info("API消息接口异常:类型:{}, {}", getErrorMsg(request), e.getMessage());

		//	接收者模式
		if (e.hasReceiveMode()) {
			//	返回自定义被动接收模式消息
			return e.getReceiveData();
		}

		ApiEnum apiEnum = e.getApiEnum();
		MsgFormat msgFormat = e.getMsgFormat();
		if (apiEnum == null && msgFormat == null) {
			return AjaxResult.error(500, "系统错误");
		}

		String apiMsg = e.getApiMsg();
		Integer apiCode = e.getApiCode();
		return AjaxResult.error(apiCode, apiMsg);
	}

	@ExceptionHandler(ChannelMarketEventMissException.class)
	public Object channelMarketEventMissException(HttpServletRequest request, HttpServletResponse response, ChannelMarketEventMissException e) {
		log.error("渠道产品接口->接入异常:类型:{}, {}", getErrorMsg(request), e.getMessage());
		String message = e.getMessage();
		return AjaxResult.error(100000, message);
	}

	@ExceptionHandler(ChannelMarketParamException.class)
	public Object ChannelMarketParamException(HttpServletRequest request, HttpServletResponse response, ChannelMarketParamException e) {
		log.error("渠道产品接口->参数异常:类型:{}, {}", getErrorMsg(request), e.getMessage());
		String message = e.getMessage();
		return AjaxResult.error(0, message);
	}

	@ExceptionHandler(ApiTestValidException.class)
	public Object apiTestValidException(HttpServletRequest request, HttpServletResponse response, ApiTestValidException e) {
		log.error("API测试接口->参数异常:类型:{}, {}", getErrorMsg(request), e.getMessage());
		String message = e.getMessage();
		return ResultVO.fail(message);
	}

	private String getErrorMsg(HttpServletRequest request) {
		StringBuilder sb = new StringBuilder()
			.append("请求方式:").append(request.getMethod()).append(", ")
			.append("地址:").append(request.getRequestURI());
		return sb.toString();
	}
}
